package com.ruoyi.system.service.impl;

import java.util.List;

import com.ruoyi.common.core.domain.entity.IssuerUnit;
import com.ruoyi.common.core.domain.entity.SealInfo;
import com.ruoyi.system.service.JmService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.ruoyi.system.mapper.IssuerUnitMapper;
import com.ruoyi.system.service.IIssuerUnitService;

/**
 * 颁发机构Service业务层处理
 *
 * @author ess
 * @date 2021-10-18
 */
@Service
public class IssuerUnitServiceImpl implements IIssuerUnitService
{
    @Autowired
    private IssuerUnitMapper issuerUnitMapper;
    @Autowired
    private JmService jmService;
    /**
     * 查询颁发机构
     *
     * @param issuerUnitId 颁发机构主键
     * @return 颁发机构
     */
    @Override
    public IssuerUnit selectIssuerUnitByIssuerUnitId(String issuerUnitId)
    {
        return getDIssuerUnit(issuerUnitMapper.selectIssuerUnitByIssuerUnitId(issuerUnitId));
    }

    /**
     * 查询颁发机构列表
     *
     * @param issuerUnit 颁发机构
     * @return 颁发机构
     */
    @Override
    public List<IssuerUnit> selectIssuerUnitList(IssuerUnit issuerUnit)
    {
        List<IssuerUnit> issuerUnits= issuerUnitMapper.selectIssuerUnitList(getEIssuerUnit(issuerUnit));
        if (issuerUnits != null && issuerUnits.size() > 0) {
            for (IssuerUnit a:issuerUnits) {
                getDIssuerUnit(a);
            }
            return issuerUnits;
        }
        return issuerUnits;
    }

    /**
     * 新增颁发机构
     *
     * @param issuerUnit 颁发机构
     * @return 结果
     */
    @Override
    public int insertIssuerUnit(IssuerUnit issuerUnit)
    {
        return issuerUnitMapper.insertIssuerUnit(getEIssuerUnit(issuerUnit));
    }

    /**
     * 修改颁发机构
     *
     * @param issuerUnit 颁发机构
     * @return 结果
     */
    @Override
    public int updateIssuerUnit(IssuerUnit issuerUnit)
    {
        return issuerUnitMapper.updateIssuerUnit(getEIssuerUnit(issuerUnit));
    }

    /**
     * 批量删除颁发机构
     *
     * @param issuerUnitIds 需要删除的颁发机构主键
     * @return 结果
     */
    @Override
    public int deleteIssuerUnitByIssuerUnitIds(String[] issuerUnitIds)
    {
        return issuerUnitMapper.deleteIssuerUnitByIssuerUnitIds(issuerUnitIds);
    }

    /**
     * 删除颁发机构信息
     *
     * @param issuerUnitId 颁发机构主键
     * @return 结果
     */
    @Override
    public int deleteIssuerUnitByIssuerUnitId(String issuerUnitId)
    {
        return issuerUnitMapper.deleteIssuerUnitByIssuerUnitId(issuerUnitId);
    }
    //加密
    private IssuerUnit getEIssuerUnit(IssuerUnit issuerUnit) {
        if (issuerUnit==null){
            return null;
        }
        if (issuerUnit.getPfxPwd() != null) {
            issuerUnit.setPfxPwd(jmService.getEncryptString(issuerUnit.getPfxPwd()));
        }
        return issuerUnit;
    }
    //解密
    private IssuerUnit getDIssuerUnit(IssuerUnit issuerUnit) {
        if (issuerUnit==null){
            return null;
        }
        if (issuerUnit.getPfxPwd() != null) {
            issuerUnit.setPfxPwd(jmService.getDecryptString(issuerUnit.getPfxPwd()));
        }
        return issuerUnit;
    }

    public IssuerUnit selectIssuerUnitSM2() {
        IssuerUnit issuerUnit = new IssuerUnit();
        issuerUnit.setAlgorithm("SM2");
        List<IssuerUnit> issuerUnits= selectIssuerUnitList(issuerUnit);
        if(issuerUnits!=null&&!issuerUnits.isEmpty()){
            return issuerUnits.get(0);
        }
        return null;
    }
}
